// 导入用户集合
const { User } = require('../../model/user');
const { RoleType } = require('../../model/roleType');

module.exports = async (req, res) => {
  const body = req.body
  const user = await User.findOne({
    username: body.username
  })
  if (!user) {
      res.status(403)
      res.json({
          message: '用户不存在！'
      })
  } else {
      if(user.password === body.password) {
        if (user.state !== 0) {
          const { username, email, roleType, _id } = user
          const role = await RoleType.findOne({
            value: roleType
          })
          res.json({
              data: {
                  _id,
                  username,
                  email, 
                  roleType,
                  role
              },
              message: '登录成功！'
          })
        } else {
          res.status(403)
          res.json({
              message: '用户已被冻结！'
          })
        }
      } else {
          res.status(403)
          res.json({
              message: '密码错误！'
          })
      }
  }
}